package com.smart.campus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smart.campus.dao.ShDepartmentMapper;
import com.smart.campus.dto.ShDepartment;
import com.smart.campus.pojo.PagedResponse;
import com.smart.campus.service.ShDepartmentService;
import com.smart.campus.util.PaginationUtil;
import com.smart.campus.dao.ShDepartmentMapper;
import com.smart.campus.dto.ShDepartment;
import com.smart.campus.service.ShDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @BelongsProject: smart-campus
 * @BelongsPackage: com.smart.campus.service.impl
 * @Author: JinBao shan
 * @date 2024/9/19 17:02
 * @Description: TODO
 * @Version: 1.0
 */

@Service
public class ShDepartmentServiceImpl implements ShDepartmentService {
    @Autowired
    private ShDepartmentMapper departmentMapper;

    @Override
    public PagedResponse<ShDepartment> findShDepartmentsPagination(ShDepartment department, Integer pageNum, Integer pageSize) {
        Page<ShDepartment> page = new Page<>(pageNum, pageSize);
        QueryWrapper<ShDepartment> wrapper = new QueryWrapper<>();
        if (department.getDeptName() != null && !department.getDeptName().isEmpty()){
            wrapper.like("dept_name",department.getDeptName());
        }
        if (department.getEnableStastus() != null){
            wrapper.eq("enable_stastus",department.getEnableStastus());
        }
        Page<ShDepartment> departmentPage = departmentMapper.selectPage(page, wrapper);
        return PaginationUtil.getPaginationResponse(departmentPage.getRecords(),pageNum,pageSize,departmentPage.getTotal());
    }

    @Override
    public List<ShDepartment> getShDepartmentList() {
        return departmentMapper.selectList(null);
    }

    @Override
    public void addShDepartment(ShDepartment department) {
        if (!department.getDeptName().isEmpty()){
            department.setEnableStastus(2);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String format = sdf.format(new java.util.Date(System.currentTimeMillis()));
            department.setCreateTime(format);
            department.setUpdateTime(format);
            departmentMapper.insert(department);
        }else {
            throw new RuntimeException();
        }
    }

    @Override
    public ShDepartment getShDepartmentById(Integer id) {
        return departmentMapper.selectById(id);
    }

    @Override
    public void updateShDepartmentById(ShDepartment department) {
        if (!department.getDeptName().isEmpty()){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String format = sdf.format(new java.util.Date(System.currentTimeMillis()));
            department.setUpdateTime(format);
            departmentMapper.updateById(department);
        }else {
            throw new RuntimeException();
        }
    }

    @Override
    public void updateShDepartmentStatusById(ShDepartment department) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(new java.util.Date(System.currentTimeMillis()));
        department.setUpdateTime(format);
        departmentMapper.updateById(department);
    }

    @Override
    public void deleteShDepartmentById(Integer id) {
        departmentMapper.deleteById(id);
    }
}
